Skip to main content
ICT
Lesson A13 - Exceptions and File I/O
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

E. Reading From a File page 7 of 12

  1. Reading textual data from a file is very similar in many ways to reading input from the keyboard. The Scanner class and all of its methods remain the same. However, you must also import the classes java.io.File and java.io.IOException. Also, the way in which you use the Scanner class constructor changes.

  1. The java.io.File is a holder class that can take a String representing the path to a file on your computer. Creating a Scanner object that reads from a file is as simple as:

    Scanner in = new Scanner(new File(“test.txt”));
    File f = new File(“C:\MyDocuments\Java\tester.txt”);
    Scanner in2 = new Scanner(f);

    The first line assumes that there is a file named “test.txt” in the directory or folder where you run your java files. The second line shows an example of creating the File object in its own line of code. It also shows the File being created with the full path to the file. This would be used if your text file was not in the same directory as your Java files. But what happens if the file that you are looking for doesn’t exist or has some other status that prevents it from being read? This causes an exception to be thrown!

  2. Scanner will take no responsibility in handling the exception, so every time that you want to use Scanner with a file, you will have to use a try-catch block. A full example is shown below:

    Scanner in;
    try{
         in = new Scanner(new File("test.txt"));
         String test = in.nextLine();
         System.out.println(test);
    }catch(IOException i){
         System.out.println("Error: " + i.getMessage());
    }

  3. When reading a large amount of data from a file, it is often useful to know whether there is any more data in the file to read. Reading from a file which has no more data will give you a NoSuchElementException and stop your program. The Scanner class has several methods for determining if there is any more data in the file to be read: hasNext(), hasNextDouble(), and ¡ will be the methods most useful for you. If there is anything still to come, hasNext() will return true, while hasNextDouble() will return true only if a valid double is next and hasNextInt() will return true only if an int value is next. Using a simple while loop, you can easily read in data until the end of a file.

    while(in.hasNext()){
          System.out.println(in.next());
    }

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.